<?php

namespace app\controllers\wx\services;

use app\models\ExpressCallback;
use app\models\order\Order;
use app\models\order\OrderLogistics;
use app\models\user\User;
use app\models\user\UserAddress;
use app\models\user\UserCommission;
use app\util\Debug;
use app\util\Request;

class Template
{
    const URL = 'http://h5.meiqianyan.com';

    public static function send($openid, $template_id, $data, $url = '')
    {
        if (!$url) {
            $url = self::URL;
        }

        $msg = [
            "touser" => $openid,
            "template_id" => $template_id,
            // "url" => $url,
            "data" => $data,
            "page" => '/sub_service/order/index'
        ];

        $token = Wx::getAccessToken();
        // $wx_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $token;
        // 小程序现在用这个
        $wx_url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $token;

        $response = Request::post($wx_url, json_encode($msg, JSON_UNESCAPED_UNICODE));
        // var_dump($response);exit;
        $response = json_decode($response, true);
        if (!isset($response['errcode']) || $response['errcode'] != 0) {
            Debug::wx('template', json_encode($response));
        }
    }

    protected static function getOpenid($user_id)
    {
        return User::find()
            ->where(['id' => $user_id])
            ->select(['openid'])
            ->scalar();
    }

    // public static function register(User $user)
    // {
    //     /*
    //      * 详细内容
    //         {{first.DATA}}
    //         用户名称：{{keyword1.DATA}}
    //         用户类型：{{keyword2.DATA}}
    //         注册时间：{{keyword3.DATA}}
    //         所属地区：{{keyword4.DATA}}
    //         {{remark.DATA}}
    //      */
    //
    //     $template_id = 'Z_5NmDhnR3YEVW_iLT1GJVH6kGsmgKzdfpEpo2xJ4u0';
    //
    //     $data = [
    //         'first' => [
    //             'value' => '恭喜,注册成功!',
    //             'color' => '#173177',
    //         ],
    //         'keyword1' => [
    //             'value' => $user['mobile'],
    //             'color' => '#173177',
    //         ],
    //         'keyword2' => [
    //             'value' => '手机注册',
    //             'color' => '#173177',
    //         ],
    //         'keyword3' => [
    //             'value' => date('Y-m-d H:i', strtotime($user['create_time'])),
    //             'color' => '#173177',
    //         ],
    //         'keyword4' => [
    //             'value' => '保密',
    //             'color' => '#173177',
    //         ],
    //         'remark' => [
    //             'value' => '点击查看详情',
    //             'color' => '#173177',
    //         ],
    //     ];
    //
    //     self::send($user['openid'], $template_id, $data);
    // }

    // public static function order(Order $order)
    // {
    //     /*
    //      * 详细内容
    //         {{first.DATA}}
    //         收件人：{{keyword1.DATA}}
    //         收件人电话：{{keyword2.DATA}}
    //         收件地址：{{keyword3.DATA}}
    //         快递员：{{keyword4.DATA}}
    //         快递员电话：{{keyword5.DATA}}
    //         {{remark.DATA}}
    //      */
    //
    //     $user = User::findOne(['id' => $order->user_id]);
    //     // $logistics = OrderLogistics::findOne(['order_id' => $order->id]);
    //     $address = UserAddress::findOne(['id' => $order->user_address]);
    //
    //     $template_id = 'aVsFk7-eGEuOG_Uo8gJ2YjIGHL6GO3xQNsRHs0cCAPM';
    //
    //     $data = [
    //         'first' => [
    //             'value' => '您的订单已支付成功',
    //             'color' => '#173177',
    //         ],
    //         'keyword1' => [
    //             'value' => base64_decode($user['nickname']),
    //             'color' => '#173177',
    //         ],
    //         'keyword2' => [
    //             'value' => $user['mobile'],
    //             'color' => '#173177',
    //         ],
    //         'keyword3' => [
    //             'value' => $address['address'],
    //             'color' => '#173177',
    //         ],
    //         'keyword4' => [
    //             'value' => '',
    //             'color' => '#173177',
    //         ],
    //         'keyword5' => [
    //             'value' => '',
    //             'color' => '#173177',
    //         ],
    //         'remark' => [
    //             'value' => '点击查看详情',
    //             'color' => '#173177',
    //         ],
    //     ];
    //
    //     self::send(self::getOpenid($order['user_id']), $template_id, $data);
    // }

    // 快递下单
    public static function expressOrder(Order $order)
    {
        /**
         * 详细内容
            提交时间
            {{time1.DATA}}

            订单类型
            {{thing2.DATA}}
         */

        // $user = User::findOne(['id' => $order->user_id]);
        $template_id = '6id_VnXyda7hnk5fDGAP15l5aigQESEZhmpt5rdouq0';

        $x = '下单成功,等待揽收';
        // if ($order->waybill) {
        //     $x = '下单成功, 运单号: ' . $order->waybill;
        // }

        $data = [
            'time1' => [
                'value' => $order->create_time,
                'color' => '#173177',
            ],
            'thing2' => [
                'value' => $x,
                'color' => '#173177',
            ],
        ];

        self::send(self::getOpenid($order['user_id']), $template_id, $data);
    }

    // 状态改变
    public static function expressStatus(ExpressCallback $express_callback)
    {
        /**
         * 订单号
        {{character_string3.DATA}}

        订单状态
        {{phrase4.DATA}}

        通知时间
        {{time5.DATA}}

        备注
        {{thing6.DATA}}

        提单号
        {{number1.DATA}}
         */

        $template_id = 'EHEUKXKC0pfL8BWkkWNLcxvsDmpZifQrWkXU0R4nLHs';

        $order = Order::findOne([
            'waybill' => $express_callback->waybill,
            'shopbill' => $express_callback->shopbill,
        ]);



        $data = [
            'character_string3' => [
                'value' => $express_callback->waybill ? $express_callback->waybill : $order->sn,
                'color' => '#173177',
            ],
            'phrase4' => [
                'value' => $express_callback->type,
                'color' => '#173177',
            ],
            'time5' => [
                'value' => $express_callback->update_time,
                'color' => '#173177',
            ],
            'thing6' => [
                'value' => '详情查看订单',
                'color' => '#173177',
            ],
            'number1' => [
                'value' => $order->sn,
                'color' => '#173177',
            ],
        ];

        self::send(self::getOpenid($order['user_id']), $template_id, $data);
    }

    // 佣金提示
    public static function commission(Order $order, $total, $user_id)
    {
        /**
        预计到账
        {{time8.DATA}}

        订单状态
        {{phrase3.DATA}}

        备注
        {{thing9.DATA}}

        收入佣金
        {{amount11.DATA}}
         */

        $template_id = 'rliP_jgTULDQsbsVO4uUV5uE6429Un0FOFjnWMXZnfw';

        $data = [
            'time8' => [
                'value' => $order->update_time,
                'color' => '#173177',
            ],
            'phrase3' => [
                'value' => '已完成',
                'color' => '#173177',
            ],
            'thing9' => [
                'value' => '已到帐',
                'color' => '#173177',
            ],
            'amount11' => [
                'value' => $total,
                'color' => '#173177',
            ],
        ];

        self::send(self::getOpenid($user_id), $template_id, $data);
    }

    // public static function commission(UserCommission $commission)
    // {
    //     /*
    //      * 详细内容
    //         {{first.DATA}}
    //
    //         佣金金额：{{keyword1.DATA}}
    //
    //         时间：{{keyword2.DATA}}
    //         {{remark.DATA}}
    //
    //      */
    //
    //     $template_id = 'cS0JNMVIqbv6NBMxZY1sURivvbrOxqRPZQFZ3e09c1w';
    //
    //     $data = [
    //         'first' => [
    //             'value' => '您有佣金到帐了',
    //             'color' => '#173177',
    //         ],
    //         'keyword1' => [
    //             'value' => $commission->balance,
    //             'color' => '#173177',
    //         ],
    //         'keyword2' => [
    //             'value' => date('Y-m-d H:i', strtotime($commission['create_time'])),
    //             'color' => '#173177',
    //         ],
    //         'remark' => [
    //             'value' => '点击查看详情',
    //             'color' => '#173177',
    //         ],
    //     ];
    //
    //     self::send(self::getOpenid($commission['user_id']), $template_id, $data);
    // }
}